package com.song.oscsong.widget;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.song.oscsong.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;

public class PagerSlidingTabStrip extends HorizontalScrollView implements View.OnClickListener{
	
	
	private ViewGroup tabsLayout;
	private ViewPager viewPager;
	private List<View> tabViews;
	private int currentPositoin;
	private Drawable slidingBlockDrawable;
	
	
	public PagerSlidingTabStrip(Context context) {
		// TODO Auto-generated constructor stub
		this(context , null);
	}

	public PagerSlidingTabStrip(Context context, AttributeSet attrs) {
		super(context, attrs);
//		TypedArray attrsTypedArray = context.obtainStyledAttributes(attrs,
//				R.styleable.PagerSlidingTabStrip);
//		slidingBlockDrawable =  
	}

	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	
		if(tabsLayout.getChildCount() <= 0)
			return;
		
		if(tabViews ==null)
			tabViews = new ArrayList<View>();
		else{
			tabViews.clear();
		}
		
		for(int w = 0;w < tabsLayout.getChildCount() ;w++)
		{
			tabViews.add(tabsLayout.getChildAt(w));
		}
		
		adjustChildWithParent(tabViews ,getMeasuredWidth() - tabsLayout.getPaddingRight() ,widthMeasureSpec ,heightMeasureSpec );
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	
	private void adjustChildWithParent(List<View> views, int parentViewWidth,
			int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
		 for(View view :views)
		 {
			 if(view.getLayoutParams() instanceof MarginLayoutParams)
			 {
				 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)  view.getLayoutParams();
				 parentViewWidth  -= lp.leftMargin +lp.rightMargin;
			 }
			 
		 }
		
		 int averageWith  = parentViewWidth / views.size();
		 int bigTabCount  = views.size();
//		 while(true)
//		 {
//			 Iterator<View> it = views.iterator();
//			 while(it.hasNext())
//			 {
//				 View view = it.next();
//				 
//			 }
//		 }
		 
		 for(View view :views)
		 {
			 if(view.getMeasuredWidth() < averageWith)
			 {
					LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
							.getLayoutParams();
					layoutParams.width = averageWith;
					view.setLayoutParams(layoutParams);
					// 再次测量让新宽度生效
					if (layoutParams instanceof MarginLayoutParams) {
						measureChildWithMargins(view, parentWidthMeasureSpec, 0,
								parentHeightMeasureSpec, 0);
					} else {
						measureChild(view, parentWidthMeasureSpec,
								parentHeightMeasureSpec);
					}
				}
		 }
		 
		 
		 
	}
	
	/**
	 *  获取布局
	 * @return
	 */
	private ViewGroup getTabsLayout()
	{
		if(tabsLayout == null)
		{
			tabsLayout = new LinearLayout(getContext());
			addView(tabsLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT ,
															ViewGroup.LayoutParams.WRAP_CONTENT));
			
		}
		
		return tabsLayout;
	}
	
	public void addTab(View v)
	{
		addTab(v , -1);
	}
	
	public void addTab(View tabView , int index)
	{
		if(tabView != null)
		{
			getTabsLayout().addView(tabView, index);
			requestLayout();
		}
	}
	
	
	/**
	 * 重载布局方法， 设置水平的布局下面的控件的监控器
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		
		ViewGroup tabViewGroup  = getTabsLayout();
		
		if(tabViewGroup !=null)
		{
			
			
			for(int w = 0; w< tabViewGroup.getChildCount() ;w++)
			{
				View itemView = tabViewGroup.getChildAt(w);
				itemView.setTag(w);
				itemView.setOnClickListener(this);
			}
			
		}
		
		
	}
	
	public void setViewPager(ViewPager viewPager)
	{
		
		this.viewPager = viewPager;
		this.viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		int index = (Integer) v.getTag();
		if(viewPager !=null)
		{
			viewPager.setCurrentItem(index, true);
		}
	}
	
}
